package com.loan.psychicloan.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.bean.LoanInfoBean;
import com.loan.psychicloan.imageloder.ImageLoader;
import com.loan.psychicloan.imageloder.ImageLoaderUtil;

/**
 * Created by watilion on 2017/12/1 0001.
 */

public class LoanInfoActivity extends AppCompatActivity  {

    private Toolbar mToolbar;
    private TextView tv_back_toolbar_title;
    private TextView tv_loanname;
    private ImageView iv_loanlogo;
    private TextView tv_loantime;
    private TextView tv_loandes;
    private TextView tv_passrate;
    private TextView tv_minamount;
    private TextView tv_maxamount;
    private TextView tv_rate;
    private TextView tv_mintime;
    private TextView tv_maxtime;
    private TextView tv_infomation;
    private Button btn_loanurl;
    private  String loanurl;
    private String mURL;
    private LoanInfoBean.DataBean mLoanInfoBean;
    ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    ImageLoader.Builder builder = new ImageLoader.Builder();
    ImageLoader imageLoader;
    TextView tv_official_accounts;
    TextView tv_wechat;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loaninfo);
        mToolbar = findViewById(R.id.back_toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        String URL = "https://www.psychicloan.com:8443/loan-api/loaninfo/getloanbyid?loanid=";
        mURL = URL + getIntent().getLongExtra("loanid", 10);
        Log.d("url", "onCreate: url" + mURL);

        initView();
        startTask();

        btn_loanurl.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){
                //Uri uri = Uri.parse(loanurl);
                Intent mIntent = new Intent(getApplicationContext(),WebViewActivity.class);
                mIntent.putExtra("loanurl",loanurl);
                startActivity(mIntent);
            }
        });




    }


    private void startTask() {
        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().getBeanOfOk(this, mURL, LoanInfoBean.class,
                new OkHttpUtils.CallBack <LoanInfoBean>() {
                    @Override
                    public void getData(LoanInfoBean loanInfoBean) {
                        if (loanInfoBean != null){
                            //如果不为空就用mLoanList接收
                            mLoanInfoBean = loanInfoBean.data;
                            setLoanInfo();


                        }
                    }
                });
    }

    private void initView(){
        tv_loanname = findViewById(R.id.tv_loanname);
        iv_loanlogo = findViewById(R.id.iv_loaninfo);
        tv_loantime = findViewById(R.id.tv_time);
        tv_loandes = findViewById(R.id.tv_loandes);
        tv_passrate = findViewById(R.id.tv_passrate);
        tv_minamount = findViewById(R.id.tv_minamount);
        tv_maxamount = findViewById(R.id.tv_maxamount);
        tv_rate = findViewById(R.id.tv_rate);
        tv_mintime = findViewById(R.id.tv_mintime);
        tv_maxtime = findViewById(R.id.tv_maxtime);
        tv_infomation = findViewById(R.id.tv_information);
        btn_loanurl = findViewById(R.id.btn_loanurl);
        tv_back_toolbar_title = findViewById(R.id.back_toolbar_title);
        tv_wechat = findViewById(R.id.tv_wechat);
        tv_official_accounts = findViewById(R.id.tv_official_accounts);

    }

    private void setLoanInfo(){
        tv_back_toolbar_title.setText(mLoanInfoBean.loanname);
        tv_loanname.setText(mLoanInfoBean.loanname+"");
        if(mLoanInfoBean.time<60) {
            tv_loantime.setText(mLoanInfoBean.time + "分钟");
        }else if (mLoanInfoBean.time>=60 && mLoanInfoBean.time<1440){
            tv_loantime.setText(mLoanInfoBean.time/60+"小时");
        }else {
            tv_loantime.setText(mLoanInfoBean.time/1440+"天");
        }
        tv_loandes.setText(mLoanInfoBean.loandes+"");
        tv_passrate.setText(mLoanInfoBean.passrate+"%");
        imageLoader =builder.url(mLoanInfoBean.loanlogo).placeHolder(R.mipmap.ic_launcher).imageView(iv_loanlogo).build();
        imageLoaderUtil.loadImage(this,imageLoader);

        if(mLoanInfoBean.minamount<10000){
            tv_minamount.setText(mLoanInfoBean.minamount+"元");
        }else{
            tv_minamount.setText(mLoanInfoBean.minamount+"万元");
        }
        if (mLoanInfoBean.maxamount<10000){
            tv_maxamount.setText(mLoanInfoBean.maxamount+"元");
        }else{
            tv_maxamount.setText(mLoanInfoBean.maxamount/10000+"万元");
        }
        tv_rate.setText(mLoanInfoBean.rate+"");
        if (mLoanInfoBean.mintime<30){
            tv_mintime.setText(mLoanInfoBean.mintime+"天");
        }else if (mLoanInfoBean.mintime>=30 && mLoanInfoBean.mintime<365){
            tv_mintime.setText(mLoanInfoBean.mintime/30+"月");
        }else{
            tv_mintime.setText(mLoanInfoBean.mintime/365+"年");
        }
        if(mLoanInfoBean.maxtime<30){
            tv_maxtime.setText(mLoanInfoBean.maxtime+"天");
        }else if (mLoanInfoBean.maxtime>=30 && mLoanInfoBean.maxtime<365){
            tv_maxtime.setText(mLoanInfoBean.maxtime/30+"月");
        }else{
            tv_maxtime.setText(mLoanInfoBean.maxtime/365+"年");
        }
        tv_infomation.setText(mLoanInfoBean.information.replace(";", "；\n"));
        loanurl = mLoanInfoBean.loanurl;
        tv_official_accounts.setText(MainActivity.WeChact.getOfficial_accounts().toString());
        tv_wechat.setText(MainActivity.WeChact.getWechat().toString());

    }


}
